package com.meeting.master.manager.impl.base;

import cn.hutool.core.collection.CollectionUtil;

import com.meeting.master.manager.UserInfoBaseDtoManager;
import com.meeting.master.manager.UserInfoDtoManager;
import com.meeting.master.manager.converter.UserInfoBaseDtoConverter;
import com.meeting.master.manager.converter.UserInfoDtoConverter;
import com.meeting.master.manager.dto.UserInfoBaseDto;
import com.meeting.master.manager.dto.UserInfoDto;
import com.meeting.master.persist.dos.UserInfo;
import com.meeting.master.persist.mapper.UserInfoDao;
import com.vs.code.AutoGenerated;
import com.vs.ox.common.exception.ErrorCode;
import com.vs.ox.common.exception.IgnoredException;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

@AutoGenerated(locked = true, uuid = "4bcb33d9-7c56-37a8-868d-663b8f2945cf")
public abstract class UserInfoDtoManagerBaseImpl implements UserInfoDtoManager {
    @AutoGenerated(locked = true)
    @Autowired
    private UserInfoBaseDtoConverter userInfoBaseDtoConverter;

    @AutoGenerated(locked = true)
    @Autowired
    private UserInfoBaseDtoManager userInfoBaseDtoManager;

    @AutoGenerated(locked = true)
    @Autowired
    private UserInfoDao userInfoDao;

    @AutoGenerated(locked = true)
    @Autowired
    private UserInfoDtoConverter userInfoDtoConverter;

    @AutoGenerated(locked = true, uuid = "14ba3227-5a32-3ee8-879f-a12b48ee750d")
    @Override
    public List<UserInfoDto> getByIds(List<Long> id) {
        /** This block is generated by vs, do not modify, start anchor 1 */
        if (CollectionUtil.isEmpty(id)) {
            return Collections.emptyList();
        }

        List<UserInfo> userInfoList = userInfoDao.getByIdsForUserInfoDto(id);
        if (CollectionUtil.isEmpty(userInfoList)) {
            return Collections.emptyList();
        }

        // 结果和入参顺序保持一致
        Map<Long, UserInfo> userInfoMap =
                userInfoList.stream().collect(Collectors.toMap(i -> i.getId(), i -> i));
        userInfoList =
                id.stream()
                        .map(i -> userInfoMap.get(i))
                        .filter(Objects::nonNull)
                        .collect(Collectors.toList());

        return doConvertFromUserInfoToUserInfoDto(userInfoList);
        /** This block is generated by vs, do not modify, end anchor 1 */
    }

    @AutoGenerated(locked = true, uuid = "7df57a73-b051-3db3-b8dd-8ba3d5ad075a")
    @Override
    public UserInfoDto getById(Long id) {
        /** This block is generated by vs, do not modify, start anchor 1 */
        List<UserInfoDto> ret = getByIds(Arrays.asList(id));
        if (ret.size() > 1) {
            throw new IgnoredException(ErrorCode.SYS_ERROR, "返回值个数超过1个");
        }
        UserInfoDto userInfoDto = CollectionUtil.isEmpty(ret) ? null : ret.get(0);
        return userInfoDto;
        /** This block is generated by vs, do not modify, end anchor 1 */
    }

    @AutoGenerated(locked = true, uuid = "805cb7fc-532b-3bd7-9089-0e47191fee3b")
    public List<UserInfoDto> doConvertFromUserInfoToUserInfoDto(List<UserInfo> userInfoList) {
        /** This block is generated by vs, do not modify, start anchor 1 */
        if (CollectionUtil.isEmpty(userInfoList)) {
            return Collections.emptyList();
        }

        List<UserInfoBaseDto> baseDtoList =
                userInfoList.stream()
                        .map(
                                tmp ->
                                        userInfoBaseDtoConverter
                                                .convertFromUserInfoToUserInfoBaseDto(tmp))
                        .collect(Collectors.toList());
        Map<Long, UserInfoDto> dtoMap =
                userInfoDtoConverter.convertFromUserInfoBaseDtoToUserInfoDto(baseDtoList).stream()
                        .collect(
                                Collectors.toMap(
                                        UserInfoDto::getId, Function.identity(), (o1, o2) -> o1));

        List<UserInfoDto> userInfoDtoList = new ArrayList<>();
        for (UserInfo i : userInfoList) {
            UserInfoDto userInfoDto = dtoMap.get(i.getId());
            if (userInfoDto == null) {
                continue;
            }

            /** This block is generated by vs, do not modify, end anchor 1 */

            /** This block is generated by vs, do not modify, start anchor 2 */
            userInfoDtoList.add(userInfoDto);
        }
        return userInfoDtoList;
        /** This block is generated by vs, do not modify, end anchor 2 */
    }
}
